/** ***********************************************************************************
* @file         com_lib.c
* @brief        系统公共函数 头文件
* @details      主要是定义了在系统中使用的公共函数 主要包含数据类型、格式的转换、MD5、校验、算法、字符比较等函数接口
* @author       杨小珛
* @date         2013/04/03
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention     函数注释不进行描述
**************************************************************************************/

#ifndef __COM_LIB_H
#define __COM_LIB_H

/*-------------------------------------------------------------------------
                       Includes
  -----------------------------------------------------------------------*/
#include "os_app.h"

#define CRC_INIT	0xffffffffL
#define COMMON_BUFFER_SIZE  1024
#define SPI_SWAP_SIZE       4096

#define DBG_ON      0x01
#define DBG_OFF     0x00

#define ALIGN_MODE_PREFIX   0
#define ALIGN_MODE_SUFFIX   1

#define BIT_RST(value, bit)        (value &= ~bit)
#define BIT_SET(value, bit)        (value |= bit)
#define BIT_CHK(value, bit)        (value & bit)

// 获取结构体成员地址偏移量
#define	ofs(type, member)     (unsigned int)(&(((type*)0)->member))

/*-------------------------------------------------------------------------
        全局变量声明(Global variable declaration)
  -----------------------------------------------------------------------*/
extern    uint32_t const MD5XSIN[64];
/*-------------------------------------------------------------------------
                       Function prototype declaration
  -----------------------------------------------------------------------*/
void MD5Digest(uint8_t* pszInput, uint8_t nInputSize, uint8_t* pszOutPut);
int mexp10( int n );

void Mem_cpy( uint8_t *Ret, uint8_t*Dat, uint32_t Length );
char *strnchar( const char * s, int c, unsigned int num );
int str_to_int_precision( unsigned char *pStr, unsigned char len, unsigned char precision );
char *str_start_with( const char *psrc, const char *prefix, uint32_t limit_cnt );
const char * byte_stream_match( const char *psrc, uint32_t s_len, const char *pmatch, uint32_t m_len );
uint32_t str_find_char_num( const char *psrc, const char TheChar, uint32_t limit_cnt );
void Task_Check_Stack( uint8_t *str, void * *TASK_Handle );
//uint32_t Str_To_HEX( uint8_t *pstr, uint16_t num );

uint16_t REV_U16( const uint16_t s );
uint32_t REV_U32( const uint32_t s );
uint32_t mstrnlen( const char *psrc, uint32_t maxlen );
uint32_t str_find_char_num( const char *psrc, const char TheChar, uint32_t limit_cnt );
void IP_Str_To_HEX( uint8_t* pHex, uint8_t* pStr );

int AsciiToHex( unsigned int asc );
unsigned int AsciiStringToHex( unsigned int mode, char* pHex, const char* pStr, unsigned int ulLen );
unsigned int Ucs2AsciiStringToHex( char* pHex, const char* pUcs2, unsigned int ulLen );
unsigned int HexToAsciiString( char* pStr, const char* pHex, unsigned int ulLen );
unsigned int AsciiHexToUcs2String( char* pUcs2, const char* pHex, unsigned int ulLen );

uint8_t check_calc_xor( const char* Data, uint16_t DateLen );
uint16_t check_calc_acc( const char* p, uint32_t len );
uint16_t check_calc_crc16( const char* AP, uint16_t LEN, uint16_t CRC16_Type );
void check_calc_md5( uint32_t *pDst, uint16_t length, uint8_t *pSrc, uint32_t ATEMP );
void check_calc_md5_from_flash( uint8_t *pDst, uint32_t ulSrc, uint32_t length, uint32_t custom );

void Split( char **arr, char *str, const char *delimiters );

int bcd_time_to_timestamp( uint32_t *ptimestamp, const char* pBcd );
int bcd_date_to_string( char *pstr, const char* pbcd );
int bcd_time_to_string( char *pstr, const char* pbcd );
int bcd_date_time_to_string( char *pstr, const char* pbcd );
void get_random_data(uint8_t* data , uint16_t len ,uint32_t time);
uint32_t calculate_crc32 (uint32_t crcInit,void *pStart, uint32_t uSize);
uint8_t calculate_crc8( uint8_t *p, uint16_t counter ); 

void array_sort (void *array, uint8_t size ,uint32_t len);
uint32_t calculated_absolute_value (uint32_t data1, uint32_t data2);
uint32_t mstrnlens( const char *psrc, char ssrc ,uint32_t maxlen );
char* mstrnstr( const char * src, long src_len, const char * substr, long substr_len );
uint8_t calc_check_sum(uint8_t* pu8Data, uint8_t u8Len);
char* memncpy( char * src, uint32_t src_len, char * substr , char findchar , uint32_t *clen );
char * get_valid_data_pointer( char * ssl , char * tcp);
void double_to_bytes( uint8_t *bytes, double data );
double bytes_to_double( uint8_t *bytes );
void float_to_bytes( uint8_t *bytes, float data );
float bytes_to_float( uint8_t *bytes );
uint32_t vin_data_verification( char *Vin );

#endif

/******************************************************************************
**                            End Of File
******************************************************************************/


